package generators;

import java.util.Vector;

import common.Settings;

import types.Menu;
import types.MenuBar;
import types.MenuItem;

public class MenuGenerator extends Generator
{


	public static void generate(MenuBar bar)
	{
		Vector<Menu> menues = bar.getMenues();
		Vector<MenuItem> items = null;
		String tmp = "";
		/*--------------------------------------------------*/
		String result = "";
		
		result += ("private int currentLoggedUserLevel = 1;\n");
		result += ("private String currentloggedUserName = \"\";\n");
		result += ("private boolean isAdmin = false;\n");
		result += ("private String serverIP = \"\";\n");
		result += ("\n\n");
		
		
		result += ("private JMenuBar mainMenuBar = null;\n");
		result += ("\n");
		for(int i=0; i < menues.size() ; i++ )
		{
			result += ("private JMenu jMenu"+ menues.get(i).getText().replace(" ", "") +" = null;\n");
		}
		result += ("\n");
		for(int i=0; i < menues.size() ; i++ )
		{
			items = menues.get(i).getItems();
			for(int j = 0 ; j < items.size() ; j++)
			{
				result += ("private JMenuItem jMenuItem"+ toFirstUpperCase(items.get(j).getText().replace(" ", "")) +" = null;\n");
			}
		}
		
		
		result += ("public int getCurrentLoggedUserLevel()\n");
		result += ("{\n");
		result += ("	return currentLoggedUserLevel;\n");
		result += ("}\n");
		result += ("\n");
		result += ("public void setCurrentLoggedUserLevel(int currentLoggedUserLevel)\n");
		result += ("{\n");
		result += ("	this.currentLoggedUserLevel = currentLoggedUserLevel;\n");
		result += ("}\n");
		result += ("\n");
		result += ("public String getCurrentloggedUserName()\n");
		result += ("{\n");
		result += ("	return currentloggedUserName;\n");
		result += ("}\n");
		result += ("\n");
		result += ("public void setCurrentloggedUserName(String currentloggedUserName)\n");
		result += ("{\n");
		result += ("	this.currentloggedUserName = currentloggedUserName;\n");
		result += ("}\n");
		
		
		result += ("/**\n");
		result += (" * This method initializes mainMenuBar\n");
		result += (" * \n");
		result += (" * @return javax.swing.JMenuBar\n");
		result += (" */\n");
		result += ("private JMenuBar getMainMenuBar()\n");
		result += ("{\n");
		result += ("	if (mainMenuBar == null)\n");
		result += ("	{\n");
		result += ("		mainMenuBar = new JMenuBar();\n");
		result += ("		mainMenuBar.setBounds(new Rectangle(1, 0, 833, 24));\n");
		for(int i=0; i < menues.size() ; i++ )
		{
			result += ("\t\tmainMenuBar.add(getJMenu"+ toFirstUpperCase(menues.get(i).getText().replace(" ", "")) +"());\n");
		}
		result += ("	}\n");
		result += ("	return mainMenuBar;\n");
		result += ("}\n\n");
		
		for(int i=0; i < menues.size() ; i++ )
		{
			tmp = toFirstUpperCase(menues.get(i).getText().replace(" ", ""));
			result += ("/**\n");
			result += (" * This method initializes jMenu"+ tmp +"\n");
			result += (" * \n");
			result += (" * @return javax.swing.JMenu\n");
			result += (" */\n");
			result += ("private JMenu getJMenu"+ tmp +"()\n");
			result += ("{\n");
			result += ("	if (jMenu"+ tmp +" == null)\n");
			result += ("	{\n");
			result += ("		jMenu"+ tmp +" = new JMenu();\n");
			result += ("		jMenu"+ tmp +".setText(\""+menues.get(i).getText()+"\");\n");
			result += ("		jMenu"+ tmp +".setActionCommand(\""+menues.get(i).getLevel()+"\");\n");
			
			items = menues.get(i).getItems();
			for(int j = 0 ; j < items.size() ; j++)
			{
				result += ("		jMenu"+ tmp +".add(getJMenuItem"+toFirstUpperCase(items.get(j).getText().replace(" ", ""))+"());\n");
			}
			
			
			result += ("	}\n");
			result += ("	return jMenu"+ tmp +";\n");
			result += ("}	\n");
		}
		
		String frm ;
		for(int i=0; i < menues.size() ; i++ )
		{
			items = menues.get(i).getItems();
			for(int j = 0 ; j < items.size() ; j++)
			{
				tmp = toFirstUpperCase(items.get(j).getText().replace(" ", ""));
				frm = items.get(j).getFormName();
				result += ("/**\n");
				result += (" * This method initializes jMenuItem"+ tmp +"\n");
				result += (" * \n");
				result += (" * @return javax.swing.JMenuItem\n");
				result += (" */\n");
				result += ("private JMenuItem getJMenuItem"+ tmp +"()\n");
				result += ("{\n");
				result += ("	if (jMenuItem"+ tmp +" == null)\n");
				result += ("	{\n");
				result += ("		jMenuItem"+ tmp +" = new JMenuItem();\n");
				result += ("		jMenuItem"+ tmp +".setText(\""+ items.get(j).getText()+"\");\n");
				result += ("		jMenuItem"+ tmp +".setActionCommand(\""+ items.get(j).getLevel()+"\");\n");
				result += ("		jMenuItem"+ tmp +".addActionListener(new java.awt.event.ActionListener()\n");
				result += ("			{\n");
				result += ("				public void actionPerformed(java.awt.event.ActionEvent e)\n");
				result += ("				{\n");
				result += ("					"+ frm +" "+ toFirstLowerCase(frm) +" = new "+ frm +"();\n");
				result += ("					"+ toFirstLowerCase(frm) +".loadForm();\n");
				result += ("				}\n");
				result += ("			});\n");
				result += ("	}\n");
				result += ("	return jMenuItem"+ tmp +";\n");
				result += ("}	\n\n");
			}
		}
		/*--------------------------------------------------*/
		result += ("// methods used for handling user levels and access rights\n");
		result += ("public void updateMenues(JMenuBar menuBar)\n");
		result += ("{\n");
		result += ("	int userLevel = getCurrentLoggedUserLevel();\n");
		result += ("\n");
		result += ("	Component[] menues = menuBar.getComponents();\n");
		result += ("\n");
		result += ("	for (int i = 0; i < menues.length; i++)\n");
		result += ("	{\n");
		result += ("		if (menues[i].getClass() == JMenu.class)\n");
		result += ("		{\n");
		result += ("			updateMenue(((JMenu) menues[i]), userLevel);\n");
		result += ("		}\n");
		result += ("	}\n");
		result += ("}\n");
		result += ("\n");
		result += ("public void updateMenue(JMenu menu, int userLevel)\n");
		result += ("{\n");
		result += ("	int menuLevel = 0;\n");
		result += ("\n");
		result += ("	try\n");
		result += ("	{\n");
		result += ("		menuLevel = Integer.parseInt(menu.getActionCommand());\n");
		result += ("	} catch (NumberFormatException ex)\n");
		result += ("	{\n");
		result += ("		// do nothing\n");
		result += ("	}\n");
		result += ("	if (menuLevel > userLevel)\n");
		result += ("	{\n");
		result += ("		menu.setEnabled(false);\n");
		result += ("	}\n");
		result += ("	else\n");
		result += ("	{\n");
		result += ("		menu.setEnabled(true);\n");
		result += ("		// Component[] menuItems = menu.getComponents();\n");
		result += ("		Component[] menuItems = menu.getMenuComponents();\n");
		result += ("		int menuItemLevel = 0;\n");
		result += ("\n");
		result += ("		for (int i = 0; i < menuItems.length; i++)\n");
		result += ("		{\n");
		result += ("			if (menuItems[i].getClass() == JMenu.class)\n");
		result += ("			{\n");
		result += ("				updateMenue(((JMenu) menuItems[i]), userLevel);\n");
		result += ("			}\n");
		result += ("			else if (menuItems[i].getClass() == JMenuItem.class)\n");
		result += ("			{\n");
		result += ("				try\n");
		result += ("				{\n");
		result += ("					menuItemLevel = Integer.parseInt(((JMenuItem) menuItems[i])\n");
		result += ("							.getActionCommand());\n");
		result += ("				} catch (NumberFormatException ex)\n");
		result += ("				{\n");
		result += ("					// do nothing\n");
		result += ("				}\n");
		result += ("				if (menuItemLevel > userLevel)\n");
		result += ("				{\n");
		result += ("					((JMenuItem) menuItems[i]).setEnabled(false);\n");
		result += ("				}\n");
		result += ("				else\n");
		result += ("				{\n");
		result += ("					((JMenuItem) menuItems[i]).setEnabled(true);\n");
		result += ("				}\n");
		result += ("			}\n");
		result += ("		}\n");
		result += ("	}\n");
		result += ("}\n");
		/*--------------------------------------------------*/
		
		// write to file
		writeToFile(Settings.MenuesOutputDirectory + Settings.MenuesSourceFile, result);
//		System.out.println(result);
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		if(!true)
		{
			MenuItem item1 = new MenuItem(1,"Open","FrmOpen");
			MenuItem item2 = new MenuItem(1,"Close","FrmClose");
			MenuItem item3 = new MenuItem(2,"Add User","FrmAddUser");
			MenuItem item4 = new MenuItem(2,"Delete User","FrmDeleteUser");
			MenuItem item5 = new MenuItem(1,"List Users","FrmListUsers");
			
			Vector<MenuItem> vec1 = new Vector<MenuItem>();
			vec1.addElement(item1);
			vec1.addElement(item2);
			
			Vector<MenuItem> vec2 = new Vector<MenuItem>();
			vec2.addElement(item3);
			vec2.addElement(item4);
			vec2.addElement(item5);
			
			Menu menu1 = new Menu(1,"File",vec1);
			Menu menu2 = new Menu(1,"Users",vec2);
			
			Vector<Menu> vec3 = new Vector<Menu>();
			vec3.addElement(menu1);
			vec3.addElement(menu2);
			
			MenuBar bar = new MenuBar(vec3);
			
			generate(bar);
		}
		else
		{
			String input = readFromFile("C:\\test.txt");
//			System.out.println(input);
//			System.out.println("===============================");
			String out = createGeneratorTemplate(input);
			System.out.println(out);
		}

	}
	

}
